package com.richrail.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.richrail.Depot;
import com.richrail.console.CLI;
import com.richrail.state.DepotState;
import com.richrail.state.TrainState;
import com.richrail.state.WagonState;
import com.richrail.util.Observable;
import com.richrail.util.Observer;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class GraphicWindow extends JPanel implements Named, Observer
{
	private static final long serialVersionUID = 1L;
	
	private JButton deleteWagonButton;
	
	private JTextField wagonNameField;
	
	private JComboBox wagonNameComboBox;
	
	private JButton newWagonButton;
	
	private JButton removeWagonButton;
	
	private TabbedView tabbedDisplay;
	
	private JLabel selectedTrainNameLabel;
	
	private JTextField selectedTrainNameField;
	
	private JButton addWagonButton;
	
	private JButton deleteTrain;
	
	private JLabel wagonNameLabel;
	
	private JButton selectTrainButton;
	
	private JComboBox trainNameComboBox;
	
	private JButton newTrainButton;
	
	private JTextField trainNameField;
	
	private JLabel trainNameLabel;
	
	private CLI cli;
	
	private String selectedTrainName;
	
	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	
	public GraphicWindow(CLI cli)
	{
		super();
		this.cli = cli;
		initGUI();
	}
	
	private void initGUI()
	{
		try
		{
			this.setPreferredSize(new java.awt.Dimension(900, 600));
			this.setLayout(null);
			{
				trainNameLabel = new JLabel();
				this.add(trainNameLabel);
				trainNameLabel.setText("Train name:");
				trainNameLabel.setBounds(12, 346, 121, 15);
			}
			{
				trainNameField = new JTextField();
				this.add(trainNameField);
				trainNameField.setBounds(145, 343, 309, 22);
				trainNameField.addKeyListener(new KeyAdapter()
				{
					@Override
					public void keyPressed(KeyEvent evt)
					{
						trainNameFieldKeyPressed(evt);
					}
				});
			}
			{
				newTrainButton = new JButton();
				this.add(newTrainButton);
				newTrainButton.setText("Make new train");
				newTrainButton.setBounds(466, 343, 164, 22);
				newTrainButton.addActionListener(new ActionListener()
				{
					@Override
					public void actionPerformed(ActionEvent evt)
					{
						newTrainButtonActionPerformed(evt);
					}
				});
			}
			{
				trainNameComboBox = new JComboBox();
				this.add(trainNameComboBox);
				trainNameComboBox.setBounds(145, 378, 309, 22);
			}
			{
				selectTrainButton = new JButton();
				this.add(selectTrainButton);
				selectTrainButton.setText("Select train");
				selectTrainButton.setBounds(466, 378, 164, 22);
				selectTrainButton.addActionListener(new ActionListener()
				{
					@Override
					public void actionPerformed(ActionEvent evt)
					{
						selectTrainButtonActionPerformed(evt);
					}
				});
			}
			{
				deleteTrain = new JButton();
				this.add(deleteTrain);
				deleteTrain.setText("Delete train");
				deleteTrain.setBounds(466, 405, 164, 22);
				deleteTrain.addActionListener(new ActionListener()
				{
					@Override
					public void actionPerformed(ActionEvent evt)
					{
						deleteTrainActionPerformed(evt);
					}
				});
			}
			{
				deleteWagonButton = new JButton();
				this.add(deleteWagonButton);
				deleteWagonButton.setText("Delete wagon");
				deleteWagonButton.setBounds(466, 566, 164, 22);
				deleteWagonButton.addActionListener(new ActionListener()
				{
					@Override
					public void actionPerformed(ActionEvent evt)
					{
						deleteWagonButtonActionPerformed(evt);
					}
				});
			}
			{
				newWagonButton = new JButton();
				this.add(newWagonButton);
				newWagonButton.setText("Make new wagon");
				newWagonButton.setBounds(466, 444, 164, 22);
				newWagonButton.addActionListener(new ActionListener()
				{
					@Override
					public void actionPerformed(ActionEvent evt)
					{
						newWagonButtonActionPerformed(evt);
					}
				});
			}
			{
				wagonNameComboBox = new JComboBox();
				this.add(wagonNameComboBox);
				wagonNameComboBox.setBounds(145, 512, 309, 22);
			}
			{
				wagonNameField = new JTextField();
				this.add(wagonNameField);
				wagonNameField.setBounds(145, 444, 309, 22);
				wagonNameField.addKeyListener(new KeyAdapter()
				{
					@Override
					public void keyPressed(KeyEvent evt)
					{
						wagonNameFieldKeyPressed(evt);
					}
				});
			}
			{
				wagonNameLabel = new JLabel();
				this.add(wagonNameLabel);
				wagonNameLabel.setText("Wagon name");
				wagonNameLabel.setBounds(12, 447, 121, 15);
			}
			{
				addWagonButton = new JButton();
				this.add(addWagonButton);
				addWagonButton.setText("Add wagon");
				addWagonButton.setBounds(466, 512, 164, 22);
				addWagonButton.addActionListener(new ActionListener()
				{
					@Override
					public void actionPerformed(ActionEvent evt)
					{
						addWagonButtonActionPerformed(evt);
					}
				});
			}
			{
				selectedTrainNameField = new JTextField();
				this.add(selectedTrainNameField);
				selectedTrainNameField.setBounds(145, 478, 309, 22);
				selectedTrainNameField.setEditable(false);
			}
			{
				selectedTrainNameLabel = new JLabel();
				this.add(selectedTrainNameLabel);
				selectedTrainNameLabel.setText("Selected train:");
				selectedTrainNameLabel.setBounds(12, 481, 121, 15);
			}
			{
				tabbedDisplay = TabbedView.create();
				this.add(tabbedDisplay);
				tabbedDisplay.setBounds(12, 12, 876, 319);
			}
			{
				removeWagonButton = new JButton();
				this.add(removeWagonButton);
				removeWagonButton.setText("Remove wagon");
				removeWagonButton.setBounds(466, 539, 164, 22);
				removeWagonButton.addActionListener(new ActionListener()
				{
					@Override
					public void actionPerformed(ActionEvent evt)
					{
						removeWagonButtonActionPerformed(evt);
					}
				});
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	private void newTrainButtonActionPerformed(ActionEvent evt)
	{
		cli.execute("new train " + trainNameField.getText());
	}
	
	private void selectTrainButtonActionPerformed(ActionEvent evt)
	{
		selectedTrainName = (String) trainNameComboBox.getSelectedItem();
		if (selectedTrainName != null)
			selectedTrainNameField.setText(selectedTrainName);
	}
	
	private void deleteTrainActionPerformed(ActionEvent evt)
	{
		String selectedTrainName = (String) trainNameComboBox.getSelectedItem();
		if (selectedTrainName != null)
			cli.execute("delete train " + selectedTrainName);
	}
	
	private void newWagonButtonActionPerformed(ActionEvent evt)
	{
		cli.execute("new wagon " + wagonNameField.getText());
	}
	
	private void addWagonButtonActionPerformed(ActionEvent evt)
	{
		String selectedWagonName = (String) wagonNameComboBox.getSelectedItem();
		if (selectedTrainName != null && selectedWagonName != null)
			cli.execute("add " + selectedWagonName + " to " + selectedTrainName);
	}
	
	private void removeWagonButtonActionPerformed(ActionEvent evt)
	{
		String selectedWagonName = (String) wagonNameComboBox.getSelectedItem();
		if (selectedTrainName != null && selectedWagonName != null)
			cli.execute("remove " + selectedWagonName + " from " + selectedTrainName);
	}
	
	private void deleteWagonButtonActionPerformed(ActionEvent evt)
	{
		String selectedWagonName = (String) wagonNameComboBox.getSelectedItem();
		if (selectedWagonName != null)
			cli.execute("delete wagon " + selectedWagonName);
	}
	
	private void trainNameFieldKeyPressed(KeyEvent evt)
	{
		if (evt.getKeyCode() == KeyEvent.VK_ENTER)
			newTrainButton.doClick();
	}
	
	private void wagonNameFieldKeyPressed(KeyEvent evt)
	{
		if (evt.getKeyCode() == KeyEvent.VK_ENTER)
			newWagonButton.doClick();
	}
	
	@Override
	public void update(Observable observable)
	{
		if (observable instanceof Depot)
		{
			DepotState depotState = ((Depot) observable).getState();
			
			List<String> trainNames = new ArrayList<String>();
			for (TrainState trainState: depotState.getTrains())
				trainNames.add(trainState.getName());
			trainNameComboBox.setModel(new DefaultComboBoxModel(trainNames.toArray()));
			
			List<String> wagonNames = new ArrayList<String>();
			for (WagonState wagonState: depotState.getWagons())
				wagonNames.add(wagonState.getName());
			wagonNameComboBox.setModel(new DefaultComboBoxModel(wagonNames.toArray()));
		}
	}
	
	@Override
	public String getName()
	{
		return "Graphical";
	}
}
